<template>
	<view>
		<view class="flex_rowC PT_3">	
			<view class="WH_40vw bg_fff flex_rowC fontS_26 B_dash PB_2" @click="chooseImg">
				<view class="" v-if="!imgList.length">
					+
				</view>
				<image v-else style="display: block;" class="WH_40vw" :src="imgList[0]" mode=""></image>
			</view>
		</view>
		
		<view class="flex_rowC">
			<view @click="submit" class="pub_bg color_fff flex_rowC PTB_2 PLR_30 B_radius_5 MT_4">
				提交
			</view>
		</view>
		
	</view>
</template>

<script>
	import {pathToBase64,base64ToPath} from '../../static/utils/js_sdk/mmmm-image-tools/index.js'
	export default {
		data() {
			return {
				imgList:[],
				imgurl:''
			}
		},
		methods: {
			upAvatar(avatar){
				let _this = this;
				this.$req('user/profile',{avatar:avatar},function(res){
					if(res.code==1){
						_this.toast('上传成功');
						setTimeout(function(){
							uni.switchTab({
								url:'./mine'
							})
						},500)
					}else{
						_this.toast(res.msg)
					}
				})
			},
			submit(){
				let _this = this;
				let url = '';
				if(!_this.imgList.length){
					this.toast('请先上传头像')
				}else{
					//alert(_this.imgList[0])
					pathToBase64(_this.imgList[0]).then(res=>{
						url = res;
						this.$req('base64Upload',{imgurl:url},function(res){
							if(res.code==1){
								_this.imgurl = res.data;
								_this.upAvatar(res.data)
							}else{
								_this.toast(res.msg)
							}
						})
					});
					
					
				}
				
				
				
			},
			chooseImg(){
				let _this = this;
				uni.chooseImage({
				    count: 1, //默认9
				    sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
				    sourceType: ['album'], //从相册选择
				    success: function (res) {
				        
						_this.imgList = res.tempFilePaths;
						console.log(JSON.stringify(_this.imgList));
				    }
				});
			},
			
		}
	}
</script>

<style>
.B_dash{
	border: 1px #CECECE dashed;
}
</style>
